#ifndef AJ_CLIENT_MANAGER_H
#define AJ_CLIENT_MANAGER_H

#include <vector>
#include <thread>
#include <QObject>
#include <QTcpServer>
#include "aj_namespace.h"
#include "aj_tcpsession.h"
#include "aj_session_threads.h"

NAMESPACE_NETAPI_BEGIN

struct ServerData
{
    uint16_t Port;
    uint32_t ThreadNum;
    void Verify()
    {
        if (Port == 0)
            Port = 12345;
        if (ThreadNum)
            ThreadNum = std::thread::hardware_concurrency();
    }
};

class TcpSever : public QTcpServer
{
    Q_OBJECT
public:
    TcpSever();
    ~TcpSever();
    //启动 非线程安全
    bool Start(ServerData &conf);
    //关闭 非线程安全
    void Stop();
    bool IsStart();
    //获取会话数
    std::vector<uint32_t> GetSessionSize() const;

public:
    //连接回调
    std::function<void(std::shared_ptr<TcpSession> &)> OnAccepted = nullptr;


protected:
    virtual void incomingConnection(qintptr handle);
    std::shared_ptr<TcpSession> CreateSession(qintptr handle);

private:
    bool m_isRunning;
    SessionThreads m_sessionThreads;
};
NAMESPACE_NETAPI_END

#endif // AJ_CLIENT_MANAGER_H
